package com.galen.wxRead.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPatch;
import com.galen.wxRead.bean.wxBean.*;
import com.galen.wxRead.bean.wxTo.*;
import com.galen.wxRead.utils.OkHttpUtils;
import com.galen.wxRead.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class WeChatGoHttpClient {

    private static final List<String> WHITELIST = Arrays.asList("weixin,fmessage,medianote,floatbottle,qmessage,qqmail,qqsafe,tmessage,weibo,filehelper".split(","));

    private static final Logger logger = LoggerFactory.getLogger(WeChatGoHttpClient.class);

    ////其他//////////////////////////////////////////////

    /**
     * getA8key
     *
     * @return
     */
    public WxGetA8KeyBean wxGetA8Key(WxGetA8KeyTo wxGetA8KeyTo) {
        WxGetA8KeyBean wxGetA8KeyBean = new WxGetA8KeyBean();
        String responseBody = "";
        String url = wxGetA8KeyTo.getNode() + "/api/Tools/GetA8Key";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("CodeType", 0);
            jsonObject.put("CodeVersion", 8);
            jsonObject.put("FontScale", 0);
            jsonObject.put("NetType", "");
            jsonObject.put("OpCode", 0);
            jsonObject.put("ReqUrl", wxGetA8KeyTo.getUrl());
            jsonObject.put("Scene", 0);
            jsonObject.put("Wxid", wxGetA8KeyTo.getKey());
            responseBody = OkHttpUtils.httpPostJson(url, JSON.toJSONString(jsonObject));
            JSONObject json = JSON.parseObject(responseBody);
            if (json != null) {
                boolean Success = json.getBoolean("Success");
                if (Success) {
                    JSONObject data = json.getJSONObject("Data");
                    if (data != null) {
                        JSONObject baseResponseObj = data.getJSONObject("BaseResponse");
                        if (baseResponseObj != null) {
                            int ret = baseResponseObj.getInteger("ret");
                            if (ret == 0) {
                                String fullUrl = data.getString("FullURL");
                                wxGetA8KeyBean.setSuccess(true);
                                wxGetA8KeyBean.setFullUrl(fullUrl);
                                JSONObject cookieObj = data.getJSONObject("Cookie");
                                String Cookie = cookieObj.getString("buffer");
                                wxGetA8KeyBean.setCookie(Cookie);
                                JSONArray HttpHeaderArray = data.getJSONArray("HttpHeader");
                                for (int i = 0; i < HttpHeaderArray.size(); i++) {
                                    JSONObject header = JSON.parseObject(HttpHeaderArray.get(i).toString());
                                    if (i == 1) {
                                        wxGetA8KeyBean.setWeChatKey(header.getString("value"));
                                    } else if (i == 2) {
                                        wxGetA8KeyBean.setWeChatUin(header.getString("value"));
                                    } else if (i == 3) {
                                        wxGetA8KeyBean.setExportKey(header.getString("value"));
                                    }
                                }
                            }
                        }
                    }
                }

            }
        } catch (Exception e) {
            System.out.println("请求链接" + url + "阅读请求体:" + jsonObject.toJSONString() + "\n阅读返回值:");
            e.printStackTrace();
        }
        return wxGetA8KeyBean;
    }

    /**
     * 刷阅读
     *
     * @param
     * @return
     */
    public WxReadBean wxReadArticle(WxReadBizArticleTo wxReadBizArticleTo) {
        WxReadBean wxReadBean = new WxReadBean();
        try {
            String url = wxReadBizArticleTo.getNode() + "/api/Coom/ReadArticle";
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Wxid", wxReadBizArticleTo.getKey());
            jsonObject.put("Url", wxReadBizArticleTo.getUrl());
            String responseBody = OkHttpUtils.httpPostJson(url, JSON.toJSONString(jsonObject));
            wxReadBean.setRet(responseBody);
            JSONObject json = JSON.parseObject(responseBody);
            if (json != null) {
                JSONObject dataObj = json.getJSONObject("Data");
                if (dataObj != null) {
                    JSONObject appmsgstatObj = dataObj.getJSONObject("appmsgstat");
                    if (appmsgstatObj != null) {
                        int readNum = appmsgstatObj.getInteger("read_num");
                        wxReadBean.setReadNum(readNum);
                        wxReadBean.setSuccess(true);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return wxReadBean;
    }


    /**
     * 心跳
     *
     * @param wxHeartTo
     * @return
     */
    public WxHeartBean wxHeartBeat(WxBaseTo wxHeartTo) {
        WxHeartBean wxHeartBean = new WxHeartBean();
        try {
            String url = wxHeartTo.getNode() + "/api/Login/HeartBeat?wxid=" + wxHeartTo.getKey();
            JSONObject jsonObject = new JSONObject();
            String responseBody = OkHttpUtils.httpPostJson(url, JSON.toJSONString(jsonObject));
            wxHeartBean.setRet(responseBody);
            if (responseBody != null) {
                if (responseBody.contains("已退出") || responseBody.contains("数据不存在") || responseBody.contains("false")) {
                    wxHeartBean.setBreak(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wxHeartBean;
    }

    public WxData62Bean wxData62Login(WxData62To wxData62To) {
        WxData62Bean wxData62Bean = new WxData62Bean();
        try {
            String url = wxData62To.getNode() + "/api/Login/62data";
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data62", wxData62To.getKey());
            jsonObject.put("password", wxData62To.getPassWord());
            jsonObject.put("userName", wxData62To.getUserName());
            String responseBody = OkHttpUtils.httpPostJson(url, JSON.toJSONString(jsonObject));
            wxData62Bean.setRet(responseBody);
            if (responseBody != null) {
                JSONObject retObj = JSON.parseObject(responseBody);
                if (retObj != null && retObj.containsKey("Data")) {
                    JSONObject dataObj = retObj.getJSONObject("Data");
                    if (dataObj != null && dataObj.containsKey("baseResponse")) {
                        JSONObject baseResponseObj = dataObj.getJSONObject("baseResponse");
                        if (baseResponseObj != null && baseResponseObj.containsKey("ret")) {
                            int ret = baseResponseObj.getInteger("ret");
                            if (ret == -106) {
                                String msg = baseResponseObj.getJSONObject("errMsg").getString("string");
                                msg = StringUtil.getSubString(msg, "secticket=", "]]></Url>");
                                wxData62Bean.setQrCode(msg);
                                wxData62Bean.setSuccess(false);
                                JSONObject sliderOCRObj = new JSONObject();
                                sliderOCRObj.put("Url",msg);
                                sliderOCRObj.put("","");
                                OkHttpUtils.httpPostJson("http://47.104.174.172:1111/SliderOCR",sliderOCRObj.toJSONString());
                            } else if (ret == 0) {
                                wxData62Bean.setSuccess(true);
                                JSONObject acctSectRespObj = dataObj.getJSONObject("acctSectResp");
                                if (acctSectRespObj != null) {
                                    String wxId = acctSectRespObj.getString("userName");
                                    wxData62Bean.setWxId(wxId);
                                }
                            }
                        }
                    }
                }
            }
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        return wxData62Bean;
    }
}
